// pages/usercenter/usercenter.js
var app = getApp();
var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
var p = require('../../../utils/password.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userinfo: [],
        phone:"",
        setphone: {
            phone: "",
            code: "",
            disabled: "",
            codemsg: "获取验证码"
        },
        img_url: c.img_url,
        center_bg:app.globalData.bg_center_01,
        contact_tel:'',
        item:{"bg_center_01":app.globalData.bg_center_01}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        utils.updateUserinfo();
        this.data.userinfo = wx.getStorageSync("userinfo");
        var that = this;
        var tel = wx.getStorageSync("userinfo").phone;
        if (tel) {
            this.setData({
              phone:tel.substr(0, 3) + '****' + tel.substr(7)
            });
        }
      var data = {
        url: "/zfcom/com/deploy",
        uuid: "contact_tel"
      };
      utils.request(data).then(function (res) {
        if (res.code == 0) {
          that.setData({
            contact_tel: res.data.contact_tel
          });
        }
      });
    },
    //下拉刷新
    onPullDownRefresh: function () {
        utils.updateUserinfo();
        utils.checkLogin();
        this.setData({
            userinfo:wx.getStorageSync("userinfo")
        });
        var tel = wx.getStorageSync("userinfo").phone;
        if (tel) {
            this.setData({
              phone:tel.substr(0, 3) + '****' + tel.substr(7)
            });
        }
        wx.stopPullDownRefresh();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    onShow: function () {
        utils.updateUserinfo();
        //utils.checkLogin();
        this.setData({
            userinfo:wx.getStorageSync("userinfo")
        });
        var tel = wx.getStorageSync("userinfo").phone;
        if (tel) {
            this.setData({
              phone:tel.substr(0, 3) + '****' + tel.substr(7)
            });
        }
    },
    err:function(){
        utils.errMsg("暂未开放");
    },
    //以下是登录方法================================================================================================================
    editPhone: function (e) {
        this.setData({
            phone: e.detail.value
        })

    },
    editCode: function (e) {
        this.setData({
            code: e.detail.value
        })

    },
    getCode: function (e) {
        var that = this;
        var phone = this.data.phone;
        var myreg = /^[1][0-9]{10}$/;
        if (phone == "" || !myreg.test(phone)) {
            wx.showToast({
                title: '手机号有误！',
                icon: 'none',
                duration: 1500
            })
            return false;
        }
        var data = {
            url: "/comv1/comcodephone/getcode",
            type: "newtel",
            phone: phone
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                that.setTime();
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1500
                })
            }
        });


    },
    bindPhone: function () {
        var phone = this.data.phone;
        var code = this.data.code;
        var openid = wx.getStorageSync('openId');
        var data = {
            url: "/v1/user/add",
            openid: openid,
            type: 0,
            phone: phone,
            code: code
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                wx.setStorageSync("userinfo", res.data)
                wx.showToast({
                    title: "绑定成功",
                    icon: 'none',
                    duration: 1500
                });
                this.onload();
                // setTimeout(function(){
                //     wx.navigateBack({
                //         delta: 1
                //     })
                // },1500);
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1500
                })
            }
        });
    },
    setTime: function () {//验证码
        var that = this;
        if (countdown == 0) {
            this.setData({
                disabled: "",
                codemsg: "获取验证码"
            })
            countdown = 60;
            return false;
        } else {
            this.setData({
                disabled: true,
                codemsg: "重新发送(" + countdown + ")"
            })
            countdown--;
        }
        setTimeout(function () {
            that.setTime();
        }, 1000);
    },
    callphone:function(){
      wx.makePhoneCall({
        phoneNumber: this.data.contact_tel //仅为示例，并非真实的电话号码
      })
    },
    //分享
    onShareAppMessage: function (res) {
      if (res.from === "button") {
        return {
          title: "育企秀",
          path: 'pages/shop/index/index?invitecode=' + this.data.userinfo.invitecode,
          imageUrl: "/static/images/Sign_in/Sign_in(2).png"
        }
      }
    },
    loginmsg: function () {
        utils.errMsg("请登录");
    }
})
